﻿using System;
using System.Collections.Generic;
using System.Linq;
using DAL.Models;

namespace DAL.DBManagers
{
    public class ClientsManager : BaseManager<Client>
    {
        public ClientsManager()
            : base()
        {
        }

        public override IEnumerable<Client> GetAll()
        {
            return base.GetAll().Where(w => w.IsActive).OrderBy(o => o.Name).ToList();
        }


        public List<ClientHelper> GetClients()
        {
            return base.GetAll()
                       .Where(w => w.IsActive && w.FinishDate == null).Select(s => new ClientHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.ClientID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive,
                           Oris_ACC_Num = s.Oris_ACC_Num,
                           CN = s.CN,
                           Association_No = s.Association_No,
                           Default_Language = s.Default_Language,
                           Country = s.Country,
                           CountryGeo = s.CountryGeo,
                           Address = s.Address,
                           AddressGeo = s.AddressGeo,
                           Postal_Code = s.Postal_Code,
                           City = s.City,
                           City_Geo = s.City_Geo,
                           TaxRegime = s.TaxRegime,
                           TaxLiability = s.TaxLiability,
                           TRN = s.TRN,
                           OwnerName = s.OwnerName,
                           OwnerNameGeo = s.OwnerNameGeo,
                           OwnerJobTitle = s.OwnerJobTitle,
                           OwnerJobTitleGeo = s.OwnerJobTitleGeo,
                           Mobile = s.Mobile,
                           Phone = s.Phone,
                           Fax = s.Fax,
                           Email1 = s.Email1,
                           CreateDate = s.CreateDate,
                           CreatedBy = s.CreatedBy,
                           LastModifyDate = s.LastModifyDate,
                           LastModifier = s.LastModifier
                       }).OrderBy(o => o.Name).ToList();
        }

        public List<ClientHelper> GetClients(string sortBy)
        {
            List<ClientHelper> prodList = base.GetAll()
                       .Where(w => w.IsActive && w.FinishDate == null).Select(s => new ClientHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.ClientID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive,
                           Oris_ACC_Num = s.Oris_ACC_Num,
                           CN = s.CN,
                           Association_No = s.Association_No,
                           Default_Language = s.Default_Language,
                           Country = s.Country,
                           CountryGeo = s.CountryGeo,
                           Address = s.Address,
                           AddressGeo = s.AddressGeo,
                           Postal_Code = s.Postal_Code,
                           City = s.City,
                           City_Geo = s.City_Geo,
                           TaxRegime = s.TaxRegime,
                           TaxLiability = s.TaxLiability,
                           TRN = s.TRN,
                           OwnerName = s.OwnerName,
                           OwnerNameGeo = s.OwnerNameGeo,
                           OwnerJobTitle = s.OwnerJobTitle,
                           OwnerJobTitleGeo = s.OwnerJobTitleGeo,
                           Mobile = s.Mobile,
                           Phone = s.Phone,
                           Fax = s.Fax,
                           Email1 = s.Email1,
                           CreateDate = s.CreateDate,
                           CreatedBy = s.CreatedBy,
                           LastModifyDate = s.LastModifyDate,
                           LastModifier = s.LastModifier
                       }).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<ClientHelper> cmp = new DAL.Models.GenericComparer<ClientHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public List<ClientHelper> GetClients(Guid ClientID)
        {
            return base.GetAll().Where(w => w.IsActive && w.ClientID == ClientID)
                       .Select(s => new ClientHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.ClientID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive,
                           Oris_ACC_Num = s.Oris_ACC_Num,
                           CN = s.CN,
                           Association_No = s.Association_No,
                           Default_Language = s.Default_Language,
                           Country = s.Country,
                           CountryGeo = s.CountryGeo,
                           Address = s.Address,
                           AddressGeo = s.AddressGeo,
                           Postal_Code = s.Postal_Code,
                           City = s.City,
                           City_Geo = s.City_Geo,
                           TaxRegime = s.TaxRegime,
                           TaxLiability = s.TaxLiability,
                           TRN = s.TRN,
                           OwnerName = s.OwnerName,
                           OwnerNameGeo = s.OwnerNameGeo,
                           OwnerJobTitle = s.OwnerJobTitle,
                           OwnerJobTitleGeo = s.OwnerJobTitleGeo,
                           Mobile = s.Mobile,
                           Phone = s.Phone,
                           Fax = s.Fax,
                           Email1 = s.Email1,
                           CreateDate = s.CreateDate,
                           CreatedBy = s.CreatedBy,
                           LastModifyDate = s.LastModifyDate,
                           LastModifier = s.LastModifier
                       }).OrderByDescending(o => o.BeginDate).ToList();
        }

        public List<ClientHelper> GetClients(Guid ClientID,string sortBy)
        {
            var prodList = base.GetAll().Where(w => w.IsActive && w.ClientID == ClientID)
                       .Select(s => new ClientHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.ClientID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive,
                           Oris_ACC_Num = s.Oris_ACC_Num,
                           CN = s.CN,
                           Association_No = s.Association_No,
                           Default_Language = s.Default_Language,
                           Country = s.Country,
                           CountryGeo = s.CountryGeo,
                           Address = s.Address,
                           AddressGeo = s.AddressGeo,
                           Postal_Code = s.Postal_Code,
                           City = s.City,
                           City_Geo = s.City_Geo,
                           TaxRegime = s.TaxRegime,
                           TaxLiability = s.TaxLiability,
                           TRN = s.TRN,
                           OwnerName = s.OwnerName,
                           OwnerNameGeo = s.OwnerNameGeo,
                           OwnerJobTitle = s.OwnerJobTitle,
                           OwnerJobTitleGeo = s.OwnerJobTitleGeo,
                           Mobile = s.Mobile,
                           Phone = s.Phone,
                           Fax = s.Fax,
                           Email1 = s.Email1,
                           CreateDate = s.CreateDate,
                           CreatedBy = s.CreatedBy,
                           LastModifyDate = s.LastModifyDate,
                           LastModifier = s.LastModifier
                       }).OrderByDescending(o => o.BeginDate).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<ClientHelper> cmp = new DAL.Models.GenericComparer<ClientHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }
        public void RepiarDates(Guid ClientID)
        {
            var clients = this.Find(f => f.IsActive && f.ClientID == ClientID).OrderBy(o => o.BeginDate).ToList();

            var count = clients.Count();

            for (int i = 0; i < count; i++)
            {
                if (i < count - 1)
                {
                    if (clients[i].FinishDate != clients[i + 1].BeginDate)
                    {
                        clients[i].FinishDate = clients[i + 1].BeginDate;
                    }
                }
                else if (i == count - 1)
                {
                    if (clients[i].FinishDate != null)
                    {
                        clients[i].FinishDate = null;
                    }
                }
            }

            SaveChanges();
        }
    }
}
